<%-- 
    Document   : addlivrosave
    Created on : 26/05/2012, 13:31:25
    Author     : Gabriel
--%>

<%@page contentType="text/html" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
    <head>
        

        <%@page import="trbg2.GerenteDeConexao" %>
        <%@page import="java.sql.*" %>

        <% trbg2.GerenteDeConexao  c = (trbg2.GerenteDeConexao)session.getAttribute("conecta"); %>

        <%
            GerenteDeConexao conecta =new GerenteDeConexao();
            session.setAttribute("conecta",conecta);
        %>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>JSP Page</title>
    </head>
    <body>
        <%
            String nome= request.getParameter("nome");
            String descricao= request.getParameter("descricao");
            String autor= request.getParameter("autor");
            String biblioteca= request.getParameter("biblioteca");
            int flag=0;
            if(nome.length()==0){
                flag++;
            }
            if(descricao.length()==0){
                flag++;
            }
            if(autor.length()==0){
                flag++;
            }
            if(biblioteca.length()==0){
                flag++;
            }
            
            if(flag==0){
                Connection con=c.getConexao();
                String query="SELECT * FROM autor WHERE nome='"+autor+"'";
                Statement stmt = con.createStatement();
                ResultSet rs2 = stmt.executeQuery(query);
                
                int idautor=0;
                if(rs2.first()==false){
                     String query2 = "INSERT INTO autor (nome)  VALUES('"+autor+"')";
                     Statement stmt2 = con.createStatement();
                      stmt2.executeUpdate(query2);
                      query="SELECT * FROM autor WHERE nome='"+autor+"'";
                      rs2 = stmt.executeQuery(query);
                      while (rs2.next()!=false) {
                        idautor=rs2.getInt("id");
                        
                    }
                }else{
                    
                    
                        idautor=rs2.getInt("id");
                        
                    
                }
                String liv ="SELECT * FROM livro WHERE nome='"+nome+"' AND biblioteca_id='"+biblioteca+"'";
                Statement stmtid = con.createStatement();
                ResultSet rs = stmtid.executeQuery(liv);
                
                if(rs.first()==false){
                    String queryadd = "INSERT INTO livro (nome, descricao, autor_id, biblioteca_id)  VALUES('"+nome+"','"+descricao+"','"+idautor+"','"+biblioteca+"')";
                    Statement stmtadd = con.createStatement();
                    int resp= stmtadd.executeUpdate(queryadd);
                    if(resp!=0){
                        out.println("Dados de Livros Incuidos com Sucesso");
                    }
                }else{
                    out.println("Livro ja existente nessa biblioteca!");
                }
                 
            }else{
                 out.println("Complete corretamente os campos!");
            }
        %>
    </body>
</html>
